package com.learn.motion.demo.fadethrough

import android.os.Bundle
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.transition.TransitionManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView
import com.learn.motion.R
import com.learn.motion.demo.MEDIUM_COLLAPSE_DURATION
import com.learn.motion.demo.MEDIUM_EXPAND_DURATION
import com.learn.motion.demo.fadeThrough
import com.learn.motion.ui.EdgeToEdge

class FadeThroughActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fade_through)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        val card: MaterialCardView = findViewById(R.id.card)
        val contact: ConstraintLayout = findViewById(R.id.card_contact)
        val cheese: ConstraintLayout = findViewById(R.id.card_cheese)
        val toggle: MaterialButton = findViewById(R.id.toggle)
        val icon: ImageView = findViewById(R.id.contact_icon)

        setSupportActionBar(toolbar)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        EdgeToEdge.setUpAppBar(findViewById(R.id.appbar), toolbar)
        EdgeToEdge.setUpScrollingContent(findViewById(R.id.content))
        Glide.with(icon).load(R.drawable.cheese_2).transform(CircleCrop()).into(icon)

        val fadeThrough = fadeThrough()
        toggle.setOnClickListener {
            if (contact.isVisible) {
                TransitionManager.beginDelayedTransition(
                    card,
                    fadeThrough.setDuration(MEDIUM_EXPAND_DURATION)
                )
                contact.isVisible = false
                cheese.isVisible = true
            } else {
                TransitionManager.beginDelayedTransition(
                    card,
                    fadeThrough.setDuration(MEDIUM_COLLAPSE_DURATION)
                )
                contact.isVisible = true
                cheese.isVisible = false
            }

        }
    }
}